/*
 * Ext GWT 2.2.5 - Ext for GWT
 * Copyright(c) 2007-2010, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */
package cn.com.company.main.client;

import cn.com.company.main.client.entity.WebbomUserinfo;
import cn.com.company.main.client.ui.BomConditionQueryWindow;
import cn.com.company.main.client.ui.BomDiffAnalysisWindow;
import cn.com.company.main.client.ui.BomForwardQueryWindow;
import cn.com.company.main.client.ui.BomPositionNoRegWindow;
import cn.com.company.main.client.ui.BomReverseQueryWindow;
import cn.com.company.main.client.ui.LoginDialog;
import cn.com.company.main.client.ui.ModifyPwdDialog;
import cn.com.company.main.client.ui.UserManagerWindow;

import com.extjs.gxt.desktop.client.Desktop;
import com.extjs.gxt.desktop.client.Shortcut;
import com.extjs.gxt.desktop.client.StartMenu;
import com.extjs.gxt.desktop.client.TaskBar;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;

public class DesktopApp {

	private Desktop desktop = new Desktop();
	private SelectionListener<MenuEvent> menuListener;
	private SelectionListener<ComponentEvent> shortcutListener;
	private BomForwardQueryWindow window1;
	private BomReverseQueryWindow window2;
	private BomConditionQueryWindow window3;
	private BomDiffAnalysisWindow window4;
	private BomPositionNoRegWindow window5;
	private UserManagerWindow window6;
	private TaskBar taskBar;
	private StartMenu menu;
	private Shortcut s1;
	private Shortcut s2;
	private Shortcut s3;
	private Shortcut s4;
	private Shortcut s5;
	private Shortcut s6;
	private MenuItem menuItem1;
	private MenuItem menuItem2;
	private MenuItem menuItem3;
	private MenuItem menuItem4;
	private MenuItem menuItem5;
	private MenuItem menuItem6;
	private MenuItem itemModPwd;
	private MenuItem itemLogout;
	private WebbomUserinfo currentUser;

	private void itemSelected(ComponentEvent ce) {
		Window w;
		if (ce instanceof MenuEvent) {
			MenuEvent me = (MenuEvent) ce;
			w = me.getItem().getData("window");
		} else {
			w = ce.getComponent().getData("window");
		}
		if (!desktop.getWindows().contains(w)) {
			desktop.addWindow(w);
		}
		if (w != null && !w.isVisible()) {
			w.show();
		} else {
			w.toFront();
		}
	}

	public void desktopInit(WebbomUserinfo entity) {
		currentUser = entity;
		menuListener = new SelectionListener<MenuEvent>() {
			@Override
			public void componentSelected(MenuEvent me) {
				itemSelected(me);
			}
		};

		shortcutListener = new SelectionListener<ComponentEvent>() {
			@Override
			public void componentSelected(ComponentEvent ce) {
				itemSelected(ce);
			}
		};

		String iconStyleName = "icon-grid";
		String title1 = "BOM多级展开";
		window1 = BomForwardQueryWindow.getInstance(title1, iconStyleName);
		s1 = new Shortcut();
		s1.setText(title1);
		s1.setId("grid-win-shortcut");
		s1.setData("window", window1);
		s1.addSelectionListener(shortcutListener);
		desktop.addShortcut(s1);

		String title2 = "BOM多级反查";
		window2 = BomReverseQueryWindow.getInstance(title2, iconStyleName);
		s2 = new Shortcut();
		s2.setText(title2);
		s2.setId("grid-win-shortcut");
		s2.setData("window", window2);
		s2.addSelectionListener(shortcutListener);
		desktop.addShortcut(s2);

		String title3 = "BOM条件查询";
		window3 = BomConditionQueryWindow.getInstance(title3);
		s3 = new Shortcut();
		s3.setText(title3);
		s3.setId("grid-win-shortcut");
		s3.setData("window", window3);
		s3.addSelectionListener(shortcutListener);
		desktop.addShortcut(s3);

		String title4 = "BOM差异分析";
		window4 = BomDiffAnalysisWindow.getInstance(title4);
		s4 = new Shortcut();
		s4.setText(title4);
		s4.setId("grid-win-shortcut");
		s4.setData("window", window4);
		s4.addSelectionListener(shortcutListener);
		desktop.addShortcut(s4);

		String title5 = "位置号录入";
		window5 = BomPositionNoRegWindow.getInstance(title5);
		s5 = new Shortcut();
		s5.setText(title5);
		s5.setId("grid-win-shortcut");
		s5.setData("window", window5);
		s5.addSelectionListener(shortcutListener);
		desktop.addShortcut(s5);

		String title6 = "用户管理";
		window6 = UserManagerWindow.getInstance(title6);
		s6 = new Shortcut();
		s6.setText(title6);
		s6.setId("acc-win-shortcut");
		s6.setData("window", window6);
		s6.addSelectionListener(shortcutListener);
		if("1".equals(currentUser.getUserpower()+"")){
			s6.setVisible(true);
		}else{
			s6.setVisible(false);
		}
		desktop.addShortcut(s6);
		
		taskBar = desktop.getTaskBar();
		taskBar.setStartButtonText("开始");

		menu = taskBar.getStartMenu();
		menu.setHeading(currentUser.getUsername());// 设置当前登录用户名
		menu.setIconStyle("user");

		menuItem1 = new MenuItem(title1);
		menuItem1.setData("window", window1);
		menuItem1.setIcon(IconHelper.createStyle("icon-grid"));
		menuItem1.addSelectionListener(menuListener);
		menu.add(menuItem1);

		menuItem2 = new MenuItem(title2);
		menuItem2.setData("window", window2);
		menuItem2.setIcon(IconHelper.createStyle("icon-grid"));
		menuItem2.addSelectionListener(menuListener);
		menu.add(menuItem2);

		menuItem3 = new MenuItem(title3);
		menuItem3.setData("window", window3);
		menuItem3.setIcon(IconHelper.createStyle("icon-grid"));
		menuItem3.addSelectionListener(menuListener);
		menu.add(menuItem3);

		menuItem4 = new MenuItem(title4);
		menuItem4.setData("window", window4);
		menuItem4.setIcon(IconHelper.createStyle("icon-grid"));
		menuItem4.addSelectionListener(menuListener);
		menu.add(menuItem4);

		menuItem5 = new MenuItem(title5);
		menuItem5.setData("window", window5);
		menuItem5.setIcon(IconHelper.createStyle("icon-grid"));
		menuItem5.addSelectionListener(menuListener);
		menu.add(menuItem5);

		menuItem6 = new MenuItem(title6);
		menuItem6.setData("window", window6);
		menuItem6.setIcon(IconHelper.createStyle("accordion"));
		menuItem6.addSelectionListener(menuListener);
		if("1".equals(currentUser.getUserpower()+"")){
			menuItem6.setVisible(true);
		}else{
			menuItem6.setVisible(false);
		}
		menu.add(menuItem6);
		
		// tools
		itemModPwd = new MenuItem("修改密码");
		itemModPwd.setIcon(IconHelper.createStyle("settings"));
		itemModPwd.addSelectionListener(new SelectionListener<MenuEvent>() {
			@Override
			public void componentSelected(MenuEvent ce) {
				ModifyPwdDialog dialog = ModifyPwdDialog.getInstance(currentUser);
				dialog.show();
			}
		});
		menu.addTool(itemModPwd);

		menu.addToolSeperator();

		itemLogout = new MenuItem("退出系统");
		itemLogout.setIcon(IconHelper.createStyle("logout"));
		itemLogout.addSelectionListener(new SelectionListener<MenuEvent>() {
			@Override
			public void componentSelected(MenuEvent ce) {
				MessageBox.confirm("提示", "确认要退出系统吗？", new Listener<MessageBoxEvent>(){
					@Override
					public void handleEvent(MessageBoxEvent be) {
						Button btn = be.getButtonClicked();
						if("yes".equalsIgnoreCase(btn.getText())){
							removeAllShortCut();
							removeAllWindow();
							taskBar.removeAll();
							taskBar.setVisible(false);
							LoginDialog dialog = LoginDialog.getInstance();
							dialog.show();
							currentUser = null;
						}
					}
				});
			}
		});
		menu.addTool(itemLogout);
	}

	private void removeAllShortCut() {
		desktop.removeShortcut(s1);
		desktop.removeShortcut(s2);
		desktop.removeShortcut(s3);
		desktop.removeShortcut(s4);
		desktop.removeShortcut(s5);
		desktop.removeShortcut(s6);
	}

	private void removeAllWindow() {
		desktop.removeWindow(window1);
		desktop.removeWindow(window2);
		desktop.removeWindow(window3);
		desktop.removeWindow(window4);
		desktop.removeWindow(window5);
		desktop.removeWindow(window6);
	}
}
